package cn.edu.cug.cs.gtl.oi.inventor.demo;

import cn.edu.cug.cs.gtl.oi.utils.Example;
import cn.edu.cug.cs.gtl.oi.utils.ViewerComponentsFactory;
import com.openinventor.inventor.viewercomponents.awt.IViewerExaminer;

import java.awt.BorderLayout;
import java.awt.Component;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;

import com.openinventor.inventor.SbBox2i32;
import com.openinventor.inventor.SbBox3f;
import com.openinventor.inventor.SbColor;
import com.openinventor.inventor.SbVec3i32;
import com.openinventor.inventor.SoPreferences;
import com.openinventor.inventor.devices.SoBufferObject;
import com.openinventor.inventor.devices.SoBufferObject.AccessModes;
import com.openinventor.inventor.devices.SoCpuBufferObject;
import com.openinventor.inventor.nodes.SoGradientBackground;
import com.openinventor.inventor.nodes.SoSeparator;
import com.openinventor.inventor.viewercomponents.awt.IViewerExaminer;
import com.openinventor.ldm.nodes.SoDataSet.DataTypes;
import com.openinventor.ldm.nodes.SoTransferFunction;
import com.openinventor.volumeviz.nodes.SoVolumeData;
import com.openinventor.volumeviz.nodes.SoVolumeRender;

public class GenericReaderDemo extends Example {
    private IViewerExaminer myViewer;

    public static void main(String[] args) {
        GenericReaderDemo example = new GenericReaderDemo();
        example.demoMain("GenericReader");
    }

    @Override
    public void start() {
        myViewer = ViewerComponentsFactory.createViewerExaminer();

        String fileName = SoPreferences.getValue("OIVJHOME") + "/data/volumeviz/bonsai2.raw";

        //data 1
        SoVolumeData pVolData = new SoVolumeData();
        pVolData.setReader(new GenericReader(fileName));

        // Use a predefined colorMap with the SoTransferFunction
        SoTransferFunction pTransFunc = new SoTransferFunction();
        pTransFunc.predefColorMap.setValue(SoTransferFunction.PredefColorMaps.GLOW);
        pTransFunc.minValue.setValue(40);
        pTransFunc.maxValue.setValue(250);

        // Node in charge of drawing the volume
        SoVolumeRender pVolRender = new SoVolumeRender();
        pVolRender.numSlicesControl.setValue(SoVolumeRender.NumSlicesControls.ALL);

        SbColor bkgColor = new SbColor(new SbColor(.5f, .5f, .5f));
        SoGradientBackground background = new SoGradientBackground();
        background.color0.setValue(bkgColor);
        background.color1.setValue(bkgColor);

        // Assemble the scene graph
        SoSeparator root = new SoSeparator();
        {
            root.addChild(background);
            root.addChild(pVolData);
            root.addChild(pTransFunc);
            root.addChild(pVolRender);
        }

        myViewer.setSceneGraph(root);
        myViewer.viewAll();

        final Component component = myViewer.getComponent();
        component.setPreferredSize(new java.awt.Dimension(600, 500));
        setLayout(new BorderLayout());
        add(component);

    }

    @Override
    public void stop() {
        myViewer.dispose();

    }
}
